package com.itlong.cloud.iot.huawei.util;

import com.itlong.cloud.POJO.DTO.common.HuawIotAuthInfoDTO;
import com.itlong.cloud.POJO.DTO.common.HuawIotRegisterDTO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.network.https.HttpsPost;
import com.itlong.cloud.utils.node.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      华为token client
 * </desc>
 *
 * @createDate 2020/01/06
 */
@Component
@RefreshScope
public class HuawTokenClient {
    @Value("${" + PlatformConstants.HUAWEI_IOT_APPID + "}")
    private String appid;
    @Value("${" + PlatformConstants.HUAWEI_IOT_SECRET + "}")
    private String secret;
    @Autowired
    HuawRequestClient huawRequestClient;
    @Autowired
    RedisService redisService;
    public String getAccessToken(){
        try {
            if(redisService.get(RedisConstant.HUAWEI_ACCESS_TOKEN_REDIS_KEY) == null){
                //获取accesstoken
                String token = this.getToken();
                redisService.set(RedisConstant.HUAWEI_ACCESS_TOKEN_REDIS_KEY, RedisConstant.HUAWEI_ACCESS_TOKEN_REDIS_CACHE_TIME, token);
                return token;
            }else{
                return redisService.get(RedisConstant.HUAWEI_ACCESS_TOKEN_REDIS_KEY);
            }
        }catch (Exception e){
            return null;
        }
    }

    /**
     * <desc>
     *      获取华为云api调用token
     * </desc>
     *
     * @return
     * @throws Exception
     * @author Jiaqi.X
     * @createDate 2020/02/13
     */
    private String getToken() throws Exception {
        //华为配置信息start
        System.out.println("【华为配置的信息为 appId】" + appid);
        //华为配置信息end
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> auth = new HashMap<>();
        Map<String,Object> identity = new HashMap<>();
        Map<String,Object> password = new HashMap<>();
        Map<String,Object> user = new HashMap<>();
        Map<String,Object> domain = new HashMap<>();
        Map<String,Object> scope = new HashMap<>();
        Map<String,Object> project = new HashMap<>();
        String[] method = new String[]{"password"};
        user.put("name",appid);
        user.put("password",secret);
        user.put("domain",domain);
        domain.put("name",appid);
        project.put("name","cn-north-4");
        scope.put("project",project);
        password.put("user",user);
        identity.put("methods",method);
        identity.put("password",password);
        auth.put("identity",identity);
        auth.put("scope",scope);
        params.put("auth",auth);
        String token = HttpsPost.doPostToken(PlatformConstants.HUAWEI_TOKEN_URL, params);
        return token;
    }

    public static void main(String[] args) throws Exception {
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> auth = new HashMap<>();
        Map<String,Object> identity = new HashMap<>();
        Map<String,Object> password = new HashMap<>();
        Map<String,Object> user = new HashMap<>();
        Map<String,Object> domain = new HashMap<>();
        Map<String,Object> scope = new HashMap<>();
        Map<String,Object> project = new HashMap<>();
        String[] method = new String[]{"password"};
        user.put("name","hw29477178");
        user.put("password","Dy86675901");
        user.put("domain",domain);
        domain.put("name","hw29477178");
        project.put("name","cn-north-4");
        scope.put("project",project);
        password.put("user",user);
        identity.put("methods",method);
        identity.put("password",password);
        auth.put("identity",identity);
        auth.put("scope",scope);
        params.put("auth",auth);
        String token = HttpsPost.doPostToken("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens", params);
        System.out.println(token);
        //注册设备
        HuawIotRegisterDTO huawIotRegisterDTO = new HuawIotRegisterDTO();
        huawIotRegisterDTO.setNode_id("0000006000000012");
        huawIotRegisterDTO.setDevice_name("0000006000000012");
        huawIotRegisterDTO.setProduct_id("5e46b062334dd4f337527744");
        HuawIotAuthInfoDTO authInfo = new HuawIotAuthInfoDTO();
        authInfo.setAuth_type("SECRET");
        huawIotRegisterDTO.setAuth_info(authInfo);
        System.out.println(JsonUtil.toJSON(huawIotRegisterDTO));
        String result = HttpsPost.doPost(token, "https://iotdm.cn-north-4.myhuaweicloud.com/v5/iot/075e638b380025032f5dc01cc8403111/devices", JsonUtil.toJSON(huawIotRegisterDTO));
        Map<String,Object> resultParams = JsonUtil.toObject(result, HashMap.class);
        Map<String,Object> resultAuthInfo = ( Map<String,Object>)resultParams.get("auth_info");
        System.out.println(resultAuthInfo.get("secret").toString());
    }
}